function initCommentBar() {
    var $comment = $('#blog-list .blog-comment'),
        height = $comment.height(),
        max = 0, nums = [];
    if (!height) {
        return false;
    }
    var $side = $('#hot-side-list');
    if ($side.length > 0) {
        max = $side.find('.blog-comment').eq(0).text();
        max = parseInt(max.replace('(', '').replace(')', ''));
    }
    $.each($comment, function (i, item) {
        var num = parseInt($(item).text());
        nums.push(num);
        max = num > max ? num : max;
    });
    $.each(nums, function (i, n) {
        var h = parseInt(n / max * height);
        h = h < 5 ? 5 : h;
        $comment.eq(i).find('.bar').height(h);
    });
    return true;
}

function goTop() {
    $("a#top").on('click', function () {
        $("html,body").animate({
            scrollTop: 0
        }, 600);
        return false;
    });
}

function initCommentForm() {
    localStorage.timeStamp = parseInt((new Date()).getTime() / 1000);
    var html = '<li class="comment-item comment-new" id="">' +
        '<img class="author-avatar" src="" alt=""/>' +
        '<p class="comment-meta">' +
        '<a class="author-name" href="#"></a>' +
        '<span class="comment-time">&nbsp;刚刚</span>' +
        '</p>' +
        '<div class="comment-content"></div>' +
        '</li>';
    $('#comment-form').on('submit', function () {
        $.ajax({
            data: $(this).serialize(),
            type: 'post',
            headers: {
                'X-Time': localStorage.timeStamp
            },
            beforeSend: function () {
                $('#comment-submit').val('正在提交').attr('disabled', 'disabled');
            },
            success: function (json) {
                var $item = $(html);
                $item.attr('id', 'comment-' + json.id).find('img').attr({
                    src: json.avatar,
                    alt: json.author
                });
                $item.find('.author-name').text(json.author);
                $item.find('.comment-content').html(json.content);
                if (json.status != 'approved') {
                    $item.find('.comment-time').after('<span class="comment-status">评论需要审核</span>')
                }
                if (json.parentId > 0) {
                    var $p = $('#comment-' + json.parentId);
                    var $children = $p.find(' > .comment-children-list');
                    if ($children[0]) {
                        $children.append($item);
                    } else {
                        $children = $('<ul class="comment-children-list"></ul>');
                        $children.append($item);
                        $p.append($children);
                    }
                } else {
                    $('ul.comment-list').append($item);
                }
                localStorage.timeStamp = parseInt((new Date()).getTime() / 1000);
                $('#comment-submit').val('休息一下');
                setTimeout(function () {
                    $('#comment-submit').val('提交').removeAttr('disabled');
                }, 5000);
                $("html,body").animate({
                    scrollTop: $('#comment-' + json.id).offset().top - 40
                }, 600);
                $('.comment-reply-cancel').trigger('click');
            }
        });
        return false;
    });
}

function initCommentReply() {
    $('.comment-reply').on('click', function (e) {
        e.preventDefault();
        var name = $(this).prevAll('.author-name').text();
        $('#comment-reply-message').hide(250, function () {
            $(this).show(250).find('span').html('回复 <strong>' + name + '</strong>');
        });
        $('#comment-parent').val($(this).attr('rel'));
        $("html,body").animate({
            scrollTop: $('#comment-form').offset().top - 80
        }, 600);
        return false;
    });
    $('.comment-reply-cancel').on('click', function (e) {
        e.preventDefault();
        $('#comment-reply-message').hide(250, function () {
            $(this).find('span').text('');
        });
        $('#comment-parent').val(0);
    })
}

$(document).ready(function () {
    goTop();
    initCommentBar();
    initCommentForm();
    initCommentReply();
});

